{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from entity import Player, Card\n",
    "from play_order.rule_based_strategy import RuleBasedStrategy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构造测试玩家，并假设其手牌已被填充若干 Card 对象\n",
    "player = Player(name=\"测试玩家\")\n",
    "\n",
    "player.hand = [\n",
    "    Card(11, \"杀\", \"♣️\", \"6\", \"基本牌\"),\n",
    "    Card(106, \"顺手牵羊\", \"♦️\", \"4\", \"锦囊牌\"),\n",
    "    Card(69, \"桃\", \"♦️\", \"2\", \"基本牌\"),\n",
    "    Card(108, \"顺手牵羊\", \"♠️\", \"4\", \"锦囊牌\"),\n",
    "    Card(95, \"决斗\", \"♦️\", \"A\", \"锦囊牌\"),\n",
    "    Card(94, \"南蛮入侵\", \"♠️\", \"K\", \"锦囊牌\"),\n",
    "    Card(50, \"闪\", \"♦️\", \"6\", \"基本牌\")\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我的初步计算是：\n",
    "\"决斗\", \"♦️\", \"A\",\n",
    "\"桃\", \"♦️\", \"2\", + 1\n",
    "\"顺手牵羊\", \"♦️\", \"4\", + 1\n",
    "\"顺手牵羊\", \"♠️\", \"4\",  + 1\n",
    "\"南蛮入侵\", \"♠️\", \"K\",  + 1\n",
    "\"闪\", \"♦️\", \"6\"（转成♠️6酒） + 1\n",
    "\"杀\", \"♣️\", \"6\",  + 1\n",
    "总共过 6牌"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳出牌顺序： [Card(69, 桃, ♦️, 2, 基本牌), Card(95, 决斗, ♦️, A, 锦囊牌), Card(106, 顺手牵羊, ♦️, 4, 锦囊牌), Card(108, 顺手牵羊, ♠️, 4, 锦囊牌), Card(94, 南蛮入侵, ♠️, K, 锦囊牌), Card(50, 闪, ♦️, 6, 基本牌), Card(11, 杀, ♣️, 6, 基本牌)]\n",
      "最高得分： 6\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "strategy = RuleBasedStrategy()\n",
    "best_order, best_score = strategy.get_optimal_play_order(player)\n",
    "print(\"最佳出牌顺序：\", best_order)\n",
    "print(\"最高得分：\", best_score)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到这个出牌似乎更好，还能避免【决斗】掉血使用不了【桃】的情况。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均运行时间：0.024695秒\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "# 假设 player 和 strategy 已经定义好\n",
    "n = 100  # 运行次数\n",
    "total_time = 0.0\n",
    "\n",
    "for _ in range(n):\n",
    "    start_time = time.perf_counter()\n",
    "    best_order, best_score = strategy.get_optimal_play_order(player)\n",
    "    end_time = time.perf_counter()\n",
    "    total_time += (end_time - start_time)\n",
    "\n",
    "average_time = total_time / n\n",
    "print(\"平均运行时间：{:.6f}秒\".format(average_time))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均运行时间：0.003585秒\n"
     ]
    }
   ],
   "source": [
    "# 测试剪枝代码的运行效率\n",
    "import time\n",
    "# 假设 player 和 strategy 已经定义好\n",
    "n = 100  # 运行次数\n",
    "total_time = 0.0\n",
    "\n",
    "for _ in range(n):\n",
    "    start_time = time.perf_counter()\n",
    "    best_order, best_score = strategy.get_optimal_play_order(player)\n",
    "    end_time = time.perf_counter()\n",
    "    total_time += (end_time - start_time)\n",
    "\n",
    "average_time = total_time / n\n",
    "print(\"平均运行时间：{:.6f}秒\".format(average_time))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "平均运行时间：0.024695秒  ==> 0.003585秒"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "charlatan",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
